
const accountStore = {
    state: {
        token: null,
        account: null,
        loginParam: {
            username: null,
            password: null,
            rem: null
        },
        pageStack: [],
    },
    mutations: {
        setAccount(state, account) {
            if (account != null) {
                localStorage.setItem('account', JSON.stringify(account));
                localStorage.setItem('account-token', account.token);
                state.account = account;
                state.token = account.token;
            }
        },
        setLoginParam(state, param) {
            if (param != null) {
                localStorage.setItem('login-param-name',  param.username);
                localStorage.setItem('login-param-pwd',  param.password);
                localStorage.setItem('login-param-rem',  param.rem);
            }
        },
        clearLoginParam(state, username) {
            localStorage.removeItem('login-param-pwd');
            localStorage.setItem('login-param-name',  username);
            localStorage.setItem('login-param-rem',  false);
        },
        loginOut(state) {
            localStorage.removeItem('account');
            localStorage.removeItem('account-token');
            state.account = null;
            state.token = null;
        },
        setResList(state, list) {
            if (list != null) {
                localStorage.setItem('account-resList', JSON.stringify(list));
                state.resList = list;
            }
        }
    },
    getters: {
        loginParam: (state) => {
            state.loginParam = {
                username: localStorage.getItem('login-param-name'),
                password: localStorage.getItem('login-param-pwd'),
                rem: localStorage.getItem('login-param-rem') == 'true'
            }
            return state.loginParam;
        },
        token: (state) => {
            if (state.token == null || state.token == 'undefined') {
                var accStr = localStorage.getItem('account');
                if (accStr != null) {
                    var account = JSON.parse(accStr);
                    state.account = account;
                    state.token = localStorage.getItem('account-token');
                }
            }
            return state.token;
        },
        account: (state) => {
            if (state.account == null) {
                var accStr = localStorage.getItem('account');
                if (accStr != null) {
                    var account = JSON.parse(accStr);
                    state.account = account;
                    state.token = localStorage.getItem('account-token');
                }
            }
            return state.account;
        },
        resList: (state) => {
            if (state.resList.length == 0) {
                var list = localStorage.getItem('account-resList');
                if (list != null) {
                    var resList = JSON.parse(list);
                    state.resList = resList;
                }
            }
            return state.resList;
        }
    }
}

export default accountStore;
